package net.yla.smartbus.core.util;

import net.yla.smartbus.module.ba.setting.entity.SettingDO;
import net.yla.smartbus.module.ba.setting.mapper.SettingDOMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;

/**
 * @author : yang lei
 * @version : 1.0
 * @date : Created in 10:46 2020/9/29
 * @description :  全局配置类
 */
@Component
public class Settings {

    @Autowired
    private SettingDOMapper settingDOMapper;

    public static Settings settings;

    @PostConstruct
    public void init() {
        settings = this;
        settings.settingDOMapper = this.settingDOMapper;
    }

    /********* 已知命名配置 ********/

    // 账号安全配置，锁定时长
    private static int aAdminLockDuration;

    // 锁定账号的错误次数阈值（账号阈值）
    private static int adminLockThreshold;

    // 锁定账号的错误次数阈值（IP阈值）
    private static int adminIpLockThreshold;

    // 获取当前平台的标识号（写死 1）
    private static int platformId = 1;

    /**
     * getInt32
     *
     * @param name
     * @return
     */
    public static Integer getInt32(String name) {
        SettingDO settingDO = settings.settingDOMapper.getInt32(name);
        return settingDO != null ? (Integer) settingDO.getValue() : null;
    }

    /**
     * getInt32 default
     *
     * @param name
     * @param defaultValue
     * @return
     */
    public static Integer getInt32(String name, Integer defaultValue) {
        SettingDO settingDO = settings.settingDOMapper.getInt32(name);
        if (settingDO != null) {
            return (Integer) settingDO.getValue();
        } else {
            return defaultValue;
        }
    }

    /**
     * getInt64
     *
     * @param name
     * @return
     */
    public static Long getInt64(String name) {
        SettingDO settingDO = settings.settingDOMapper.getInt64(name);
        return settingDO != null ? (Long) settingDO.getValue() : null;
    }

    /**
     * getInt64 default
     *
     * @param name
     * @param defaultValue
     * @return
     */
    public static Long getInt64(String name, Long defaultValue) {
        SettingDO settingDO = settings.settingDOMapper.getInt64(name);
        if (settingDO != null) {
            return (Long) settingDO.getValue();
        } else {
            return defaultValue;
        }
    }

    /**
     * getString
     *
     * @param name
     * @return
     */
    public static String getString(String name) {
        SettingDO settingDO = settings.settingDOMapper.getString(name);
        return settingDO != null ? (String) settingDO.getValue() : null;
    }

    /**
     * getString default
     *
     * @param name
     * @param defaultValue
     * @return
     */
    public static String getString(String name, String defaultValue) {
        SettingDO settingDO = settings.settingDOMapper.getString(name);
        if (settingDO != null) {
            return (String) settingDO.getValue();
        } else {
            return defaultValue;
        }
    }

    /**
     * getDouble
     *
     * @param name
     * @return
     */
    public static Double getDouble(String name) {
        SettingDO settingDO = settings.settingDOMapper.getDouble(name);
        return settingDO != null ? Double.parseDouble(settingDO.getValue().toString()) : null;
    }

    /**
     * getDouble default
     *
     * @param name
     * @param defaultValue
     * @return
     */
    public static Double getDouble(String name, Double defaultValue) {
        SettingDO settingDO = settings.settingDOMapper.getDouble(name);
        if (settingDO != null) {
            return Double.parseDouble(settingDO.getValue().toString());
        } else {
            return defaultValue;
        }
    }

    /**
     * getDateTime
     *
     * @param name
     * @return
     */
    public static Date getDateTime(String name) {
        SettingDO settingDO = settings.settingDOMapper.getDateTime(name);
        return settingDO != null ? (Date) settingDO.getValue() : null;
    }

    /**
     * getDateTime default
     *
     * @param name
     * @param defaultValue
     * @return
     */
    public static Date getDateTime(String name, Date defaultValue) {
        SettingDO settingDO = settings.settingDOMapper.getDateTime(name);
        if (settingDO != null) {
            return (Date) settingDO.getValue();
        } else {
            return defaultValue;
        }
    }


    // 获取指定作用 URL 域和配置名称的配置值。找不到匹配 URL 地址的，则加载 URL 地址为 null 的。

    /**
     * @param name
     * @param url
     * @return
     */
    public static Integer getInt32ByUrl(String name, String url) {
        SettingDO settingDO = settings.settingDOMapper.getInt32ByUrl(name, url);
        if (settingDO == null) {
            return getInt32(name);
        } else {
            return (Integer) settingDO.getValue();
        }
    }

    /**
     * @param name
     * @param url
     * @param defaultValue
     * @return
     */
    public static Integer getInt32ByUrl(String name, String url, Integer defaultValue) {
        SettingDO settingDO = settings.settingDOMapper.getInt32ByUrl(name, url);
        if (settingDO == null) {
            SettingDO settingDO1 = settings.settingDOMapper.getInt32(name);
            if (settingDO1 != null) {
                return (Integer) settingDO1.getValue();
            } else {
                return defaultValue;
            }
        } else {
            return (Integer) settingDO.getValue();
        }
    }

    /**
     * @param name
     * @param url
     * @return
     */
    public static Long getInt64ByUrl(String name, String url) {
        SettingDO settingDO = settings.settingDOMapper.getInt64ByUrl(name, url);
        if (settingDO == null) {
            return getInt64(name);
        } else {
            return (Long) settingDO.getValue();
        }
    }

    /**
     * @param name
     * @param url
     * @param defaultValue
     * @return
     */
    public static Long getInt64ByUrl(String name, String url, Long defaultValue) {
        SettingDO settingDO = settings.settingDOMapper.getInt64ByUrl(name, url);
        if (settingDO == null) {
            SettingDO settingDO1 = settings.settingDOMapper.getInt64(name);
            if (settingDO1 != null) {
                return (Long) settingDO1.getValue();
            } else {
                return defaultValue;
            }
        } else {
            return (Long) settingDO.getValue();
        }
    }

    /**
     * @param name
     * @param url
     * @return
     */
    public static String getStringByUrl(String name, String url) {
        SettingDO settingDO = settings.settingDOMapper.getStringByUrl(name, url);
        if (settingDO == null) {
            return getString(name);
        } else {
            return (String) settingDO.getValue();
        }
    }

    /**
     * @param name
     * @param url
     * @param defaultValue
     * @return
     */
    public static String getStringByUrl(String name, String url, String defaultValue) {
        SettingDO settingDO = settings.settingDOMapper.getStringByUrl(name, url);
        if (settingDO == null) {
            SettingDO settingDO1 = settings.settingDOMapper.getString(name);
            if (settingDO1 != null) {
                return (String) settingDO1.getValue();
            } else {
                return defaultValue;
            }
        } else {
            return (String) settingDO.getValue();
        }
    }

    /**
     * @param name
     * @param url
     * @return
     */
    public static Double getDoubleByUrl(String name, String url) {
        SettingDO settingDO = settings.settingDOMapper.getDoubleByUrl(name, url);
        if (settingDO == null) {
            return getDouble(name);
        } else {
            return Double.parseDouble(settingDO.getValue().toString());
        }
    }

    /**
     * @param name
     * @param url
     * @param defaultValue
     * @return
     */
    public static Double getDoubleByUrl(String name, String url, Double defaultValue) {
        SettingDO settingDO = settings.settingDOMapper.getDoubleByUrl(name, url);
        if (settingDO == null) {
            SettingDO settingDO1 = settings.settingDOMapper.getDouble(name);
            if (settingDO1 != null) {
                return Double.parseDouble(settingDO1.getValue().toString());
            } else {
                return defaultValue;
            }
        } else {
            return Double.parseDouble(settingDO.getValue().toString());
        }
    }

    /**
     * @param name
     * @param url
     * @return
     */
    public static Date getDateTimeByUrl(String name, String url) {
        SettingDO settingDO = settings.settingDOMapper.getDateTimeByUrl(name, url);
        if (settingDO == null) {
            return getDateTime(name);
        } else {
            return (Date) settingDO.getValue();
        }
    }

    /**
     * @param name
     * @param url
     * @param defaultValue
     * @return
     */
    public static Date getDateTimeByUrl(String name, String url, Date defaultValue) {
        SettingDO settingDO = settings.settingDOMapper.getDateTimeByUrl(name, url);
        if (settingDO == null) {
            SettingDO settingDO1 = settings.settingDOMapper.getDateTime(name);
            if (settingDO1 != null) {
                return (Date) settingDO1.getValue();
            } else {
                return defaultValue;
            }
        } else {
            return (Date) settingDO.getValue();
        }
    }


    // 设置配置
    public static void SetValue(String name, Object value) {
        SettingDO settingDO = settings.settingDOMapper.getByName(name);
        if (settingDO != null) {
            settings.settingDOMapper.insertSetting(name, value);
        } else {
            settings.settingDOMapper.updateSetting(name, value);
        }
    }

    public static void SetValue(String name, String url, Object value) {
        SettingDO settingDO = settings.settingDOMapper.getByUrl(name, url);
        if (settingDO != null) {
            settings.settingDOMapper.insertSettingByUrl(name, url, value);
        } else {
            settings.settingDOMapper.updateSettingByUrl(name, url, value);
        }
    }

}
